# -*- coding: UTF-8 -*-


class Router(object):

    def __init__(self, core):  
        self.core = core


    def __executeCmd(self, argList):
        cmdArgNum = len(argList)

        if cmdArgNum == 2:
            controller, method = self.__convertCmdController(argList[1]), 'execute'
        elif cmdArgNum == 3:
            controller, method = self.__convertCmdController(argList[1]), argList[2].strip()
        else:
            controller = method = ''

        if not controller or not method:
            return

        ins = self.core.loader.instance('controller.%s' % controller, controller, transport_core = True)
        if hasattr(ins, method):
            getattr(ins, method)()
        else:
            raise Exception(u'controller[%s]不存在method[%s]' % [controller, method])


    def __convertCmdController(self, controller):
        return ''.join([v.capitalize() for v in controller.strip().split('_')])


    def __call__(self):
        sys = self.core.loader.module('sys')
        if len(sys.argv) > 0:
            self.__executeCmd(sys.argv)
        else:
            pass
